package com.huoyun.base.action;

import java.util.ArrayList;
import java.util.List;

import com.huoyun.base.bean.Region;
import com.huoyun.base.service.RegionService;

public class RegionAction extends BaseAction
{

	private RegionService regionService;
	
	private String code;
	
	private List<Region> regions;
	
	private String strRegion;
	
	public String showRegion()
	{
		List<Region> regions = new ArrayList<Region>();
		
		List<Region> hotRegions = regionService.queryHotCitys();
		StringBuffer str = new StringBuffer("{");
		Region parentRegion = null;
		for(Region region : hotRegions)
		{
			parentRegion = regionService.getRegionById(region.getParentId());
			str.append("\"" + region.getName() + "\":{\"code\":\"" + region.getCode() + "\",\"parent\":{\"name\":\""+parentRegion.getName() + "\",\"code\":\"" + parentRegion.getCode() + "\"}}," );
		}
		
		List<Region> pRegions = regionService.queryChilds(1);
		
		int m = 0;
		for(Region region : pRegions)
		{
			m++;
			str.append("\"" + region.getName() + "\":{\"scope\":\"" + region.getScope() + "\",\"code\":\"" + region.getCode()+"\",\"s\":[\""+region.getEnName()+"\",\""+region.getShortEnName()+"\"],");
			List<Region> cRegions = regionService.queryChilds(region.getId());
			
			int j = 0;
			for(Region cregion : cRegions)
			{
				j++;
				
				str.append("\"" + cregion.getName() + "\":{\"code\":\"" + cregion.getCode()+"\",\"s\":[\""+cregion.getEnName()+"\",\""+cregion.getShortEnName()+"\"],");
				
				List<Region> tRegions = regionService.queryChilds(cregion.getId());
				
				int i = 0;
				for(Region tregion : tRegions)
				{
					i++;
					
					str.append("\"" + tregion.getName() + "\":{\"code\":\"" + tregion.getCode()+"\",\"s\":[\""+tregion.getEnName()+"\",\""+tregion.getShortEnName()+"\"]}");
					
					if(i != tRegions.size())
					{
						str.append(",");
					}
				}
				if(j == cRegions.size())
				{
					str.append("}");
				}
				else
				{
					str.append("},");
				}
			}
			
			if(m == pRegions.size())
			{
				str.append("}");
			}
			else
			{
				str.append("},");
			}
		}
		
		str.append("}");
		
		strRegion = str.toString();
		
		return SUCCESS;
	}
	
	public String getCitys() {
		regions = regionService.queryChilds(code);
		return SUCCESS;
	}

	public String getTowns() {
		regions = regionService.queryChilds(code);
		return SUCCESS;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public String getStrRegion() {
		return strRegion;
	}

	public void setStrRegion(String strRegion) {
		this.strRegion = strRegion;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public List<Region> getRegions() {
		return regions;
	}

	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}
	
	
}
